package com.desksoft.ecommerce.util.html.element.format;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;

import com.desksoft.ecommerce.util.html.PageXml;
import com.desksoft.ecommerce.util.html.element.PageElement;
import com.desksoft.ecommerce.util.html.element.format.colname.ColName;
import com.desksoft.ecommerce.util.html.element.format.dataformat.DataFormat;
import com.desksoft.ecommerce.util.html.element.format.dataformat.DateFormat;
import com.desksoft.ecommerce.util.html.element.format.dataformat.EnumFormat;
import com.desksoft.ecommerce.util.html.element.format.dataformat.NumberFormat;
import com.desksoft.ecommerce.util.html.element.format.dataformat.PriceFormat;
import com.desksoft.ecommerce.util.html.element.query.QuerySqlElement;

/**
 * 显示数据格式化
 * @author Administrator
 *
 */
public class DisplayFormat extends PageElement implements Serializable {

	protected String tableId;
	
	protected String width;
	protected String height;
	
	protected List<DataFormat> formatList;
	protected List<ColName> colNameList;
	
	protected PageXml pageXml;
	
	public DisplayFormat(Element elmt, PageXml pageXml) {
		super();
		this.pageXml = pageXml;
		this.tableId = elmt.attributeValue("tableId");
		this.width = elmt.attributeValue("width");
		if (this.width == null){this.width = "";}
		this.height = elmt.attributeValue("height");
		if (this.height == null){this.height = "";}
		
		this.formatList = this.parse(elmt.elements("format"));
		this.colNameList = this.parseColName(elmt.element("col-name"));
	}
	
	/**
	 * 解析数据格式化
	 */
	public List<DataFormat> parse(List<Element> elementL){
		List<DataFormat> formatL = new ArrayList<DataFormat>(elementL.size());
		for (Element elmt : elementL){
			if ("date".equals(elmt.attributeValue("type"))){
				formatL.add(new DateFormat(elmt));
			}
			if ("enum".equals(elmt.attributeValue("type"))){
				formatL.add(new EnumFormat(elmt));
			}
			if ("number".equals(elmt.attributeValue("type"))){
				formatL.add(new NumberFormat(elmt));
			}
			if ("price".equals(elmt.attributeValue("type"))){
				formatL.add(new PriceFormat(elmt));
			}
		}
		return formatL;
	}
	
	/**
	 * 解析列名显示元素
	 */
	public List<ColName> parseColName(Element elmtColName){
		if (elmtColName == null){
			return new ArrayList<ColName>();
		}
		List<Element> elmtL = elmtColName.elements("column");
		List<ColName> colNameL = new ArrayList<ColName>(elmtL.size());
		
		for (Element elmt : elmtL){
			String name = elmt.attributeValue("name"),
					display = elmt.attributeValue("display");
			colNameL.add(new ColName(name, display));
		}
		return colNameL;
	}

	public String getTableId() {
		return tableId;
	}

	public void setTableId(String tableId) {
		this.tableId = tableId;
	}

	public List<DataFormat> getFormatList() {
		return formatList;
	}

	public void setFormatList(List<DataFormat> formatList) {
		this.formatList = formatList;
	}

	public List<ColName> getColNameList() {
		return colNameList;
	}

	public void setColNameList(List<ColName> colNameList) {
		this.colNameList = colNameList;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public PageXml getPageXml() {
		return pageXml;
	}

	public void setPageXml(PageXml pageXml) {
		this.pageXml = pageXml;
	}
}
